package edu.xjtu.jih.showDemands;

import edu.xjtu.jih.common.Result;
import edu.xjtu.jih.common.Utils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;

@WebServlet({
        "/Show_demands_info",
        "/Demands_info_by_label"
})
public class ShowDemandsAPI extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        try {
            String path = request.getServletPath();//获取请求路径
            if ("/Show_demands_info".equals(path)) {
                //System.out.println(0);
                try {
                    ShowDemandsService showDemandsService = new ShowDemandsService();
                    //System.out.println(1);
                    List<ShowDemandsDto> demandsList = showDemandsService.getDemandsInfo();
                    //System.out.println(2);
                    Utils.outResult(response, Result.success(demandsList));
                    //System.out.println(3);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            /*else if ("/Demands_info_by_label".equals(path)) {
                Reader reader = request.getReader();
                CharArrayWriter out = new CharArrayWriter();//该输出流将数据输出到一个字符数组中
                try {
                    char[] charBuff = new char[1024];
                    int len = -1;//每次从输入流实际读取的字符数

                    while ((len = reader.read(charBuff)) != -1) {
                        out.write(charBuff, 0, len);
                    }
                    out.flush();

                    String label = out.toString();
                    ShowDemandsService showDemandsService = new ShowDemandsService();
                    List<ShowDemandsDto> demandsList = showDemandsService.getDemandsInfoByLabel(label);
                    System.out.println(3);
                    Utils.outResult(response,Result.success(demandsList));
                    System.out.println(4);

                }catch (Exception e){
                    e.printStackTrace();
                }
            }*/
        }catch (Exception e){
            e.printStackTrace();
        }
        finally {

        }
    }
}
